package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetCollectFavoritesBean;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 15/12/21.
 */
public class CollectInfoAdapter extends MyAdapter<GetCollectFavoritesBean.DataEntity> {

    public Map<Integer, Boolean> map;

    public boolean isSetAllSelect() {
        return setAllSelect;
    }

    public void setSetAllSelect(boolean setAllSelect) {
        this.setAllSelect = setAllSelect;
    }

    boolean setAllSelect = false;

    public boolean isShowSelect() {
        return showSelect;
    }

    public void setShowSelect(boolean showSelect) {
        this.showSelect = showSelect;
    }

    CheckBox mainCheckBox;

    boolean showSelect = false;

    public Map<Integer, Boolean> getMap() {
        return map;
    }

    public void setMap(Map<Integer, Boolean> map) {
        this.map = map;
    }

    public CollectInfoAdapter(Context context, List<GetCollectFavoritesBean.DataEntity> list, CheckBox mainCheckBox) {
        super(context, list);
        map = new HashMap<>();
        this.mainCheckBox = mainCheckBox;

        for (int i = 0; i < list.size(); i++) {
            map.put(i, false);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_collect_info, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String icon = list.get(position).getImg().get(0);
        DeviceUtil.Log("icon = " + icon);
        if (icon == null || icon.isEmpty()) {
            holder.collectInfoIcon.setVisibility(View.GONE);
        } else {
            Glide.with(context).load(list.get(position).getImg().get(0)).into(holder.collectInfoIcon);
        }
        holder.collectInfoTitle.setText(list.get(position).getTitle());
        holder.collectInfoContent.setText(list.get(position).getContent());

        holder.collectInfoSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                map.put(position, isChecked);
                if (!isChecked && setAllSelect == true) {
                    setAllSelect = false;
                    mainCheckBox.setChecked(false);
                    DeviceUtil.Log("position = " + position);
                }
                DeviceUtil.Log("position = "  + position + "  boolean = " + map.get(position));
            }
        });

        if (showSelect) {
            holder.collectInfoSelect.setVisibility(View.VISIBLE);
            holder.collectInfoSelect.setChecked(setAllSelect);
        } else {
            holder.collectInfoSelect.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.collect_info_select)
        CheckBox collectInfoSelect;
        @Bind(R.id.collect_info_icon)
        ImageView collectInfoIcon;
        @Bind(R.id.collect_info_title)
        TextView collectInfoTitle;
        @Bind(R.id.collect_info_content)
        TextView collectInfoContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
